package com.it.chinasoft.service.activiti;


import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.task.Task;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.List;

public class ApplicationFlow {
    /**
     * 获取默认流程引擎实例，会自动读取activiti.cfg.xml文件
     */
    private ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    private final static Logger logger = LoggerFactory.getLogger(ApplicationFlow.class);
    /**
     * 部署流程定义
     */
    @Test
    public void deployFlow() throws FileNotFoundException, FileNotFoundException {
        RepositoryService repositoryService = processEngine.getRepositoryService();

        FileInputStream bpmnStream = new FileInputStream("D:\\Programming\\Projects\\prusa\\src\\main\\resources\\processes\\application.xml");
        //得到流程引擎
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        processEngine.getRepositoryService()
                .createDeployment()
                .name("员工发起请假")
                .addInputStream("applicationbpmn", bpmnStream)
                .deploy();
        //注意：inputStream不能带后缀，FileInputStream用全称。
        logger.info("部署工作流成功！");
    }

    /**
     * 启动刚刚部署的流程。
     */
    @Test
    public void StartProcessInstance(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        processEngine.getRuntimeService()
                .startProcessInstanceById("myProcess_1:1:67504");  //这个是act_re_procdef表ID
        logger.info("启动工作流");
    }

    /**
     * 申请者查询当前正在执行的任务
     */
    @Test
    public void applierQueryTask(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        List<Task> tasks = processEngine.getTaskService()
                .createTaskQuery()
                .taskAssignee("Nike")
                .list();
        for (Task task : tasks) {
            logger.info("代办任务ID:"+task.getId());
            logger.info("代办任务name:"+task.getName());
            logger.info("代办任务创建时间:"+task.getCreateTime());
            logger.info("代办任务办理人:"+task.getAssignee());
            logger.info("流程实例ID:"+task.getProcessInstanceId());
            logger.info("执行对象ID:"+task.getExecutionId());
        }
    }

    /**
     * 申请者完成节点任务
     */
    @Test
    public void applierFinishTask(){
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        engine.getTaskService()
                .complete("77505"); //查看act_ru_task数据表id
        logger.info("Nike提交完成申请，轮到Dick对其进行审批。");
    }

    /**
     * 主管Dick查询自己的任务
     */
    @Test
    public void managerQueryTask(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        List<Task> tasks = processEngine.getTaskService()
                .createTaskQuery()
                .taskAssignee("Dick")
                .list();
        for (Task task : tasks) {
            logger.info("待办任务名称："+task.getName());
            logger.info("代办任务ID:"+task.getId());
            logger.info("代办任务name:"+task.getName());
            logger.info("代办任务创建时间:"+task.getCreateTime());
            logger.info("代办任务办理人:"+task.getAssignee());
            logger.info("流程实例ID:"+task.getProcessInstanceId());
            logger.info("执行对象ID:"+task.getExecutionId());
        }
    }

    /**
     * 管理者结束任务
     */
    @Test
    public void managerFinishTask(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        processEngine.getTaskService()
                .complete("72502");  //查看act_ru_task数据表
        logger.info("审批者结束任务。");
    }




}
